<?php
/**
 * 加群小程序微信群类文件
 * 作者：刘单风
 * 最后修改时间：2019-01-16
 * 版权：医库PHP小组
 */

namespace app\model;
use think\Model;
use think\Db;

class Wechatgroups extends Model
{
    //获取列表数据
    function groupList($where,$capge)
    {
        $data = Db::name('wechat_groups')
            ->field('id,group_name,group_img,group_notice,code_img,helper_code_img')
            ->where($where)
            ->order('id desc')
            ->page($capge, 10)
            ->select();
        return $data;
    }

    //获取群聊详情
    public function groupDetail($groupid)
    {
        $detail = Db::name('wechat_groups')
            ->where('id', $groupid)
            ->find();
        //查看是否过期
        if($detail['deadline']<=time()){
            $detail['is_overdue']=1;
        }else{
            if($detail['scan_num']>=100){
                //超过100人只能群助手进群
                $detail['is_overdue']=1;
            }else{
                $detail['is_overdue']=0;
            }
        }
        //更新扫码次数
        Db::table('wechat_groups')
            ->where('id',$groupid)
            ->inc('scan_num')
            ->update();
        return $detail;
    }


    //科室列表数据
    public function deplist($datatype)
    {
        if (empty($datatype)) {
            $depdata = Db::name('wechat_deps')
                ->field('id,dep_name')
                ->where('father_id', 0)
                ->where('data_type', 0)
                ->select();
            for ($i = 0; $i < count($depdata); $i++) {
                //获取子级科室数据
                $child = Db::name('wechat_deps')
                    ->field('id,dep_name')
                    ->where('father_id', $depdata[$i]['id'])
                    ->where('data_type', 0)
                    ->select();
                $depdata[$i]['childs'] = $child;
            }
        } else {
            $depdata = Db::name('wechat_deps')
                ->field('id,dep_name')
                ->where('father_id!=0')
                ->where('data_type', 0)
                ->select();
            for ($i = 0; $i < count($depdata); $i++) {
                //获取疾病
                $child = Db::name('wechat_deps')
                    ->field('id,dep_name AS dis_name')
                    ->where('father_id', $depdata[$i]['id'])
                    ->where('data_type', 1)
                    ->select();
                $depdata[$i]['childs'] = $child;
            }
        }
        return $depdata;
    }

    //城市列表数据
    public function citylist()
    {
        $citydata = Db::name('wechat_citys')
            ->field('id,city_name')
            ->where('father_id', 0)
            ->where('is_show',1)
            ->select();
        for ($i = 0; $i < count($citydata); $i++) {
            //获取子级科室数据
            $child = Db::name('wechat_citys')
                ->field('id,city_name')
                ->where('is_show',1)
                ->where('father_id', $citydata[$i]['id'])
                ->select();
            $citydata[$i]['childs'] = $child;
        }
        return $citydata;
    }

    //获取二级科室id集合
    public function childdeps($depid)
    {
        //获取子级科室数据
        $child = Db::name('wechat_deps')
            ->field('id')
            ->where('father_id', $depid)
            ->column('id');
        $child=implode(",",$child);
        return $child;
    }

    //banner数据信息
    public function bannerdata($datatype=0)
    {
        $data = Db::name('wechat_banners')
            ->where('data_type', $datatype)
            ->order('id desc')
            ->select();
        return $data;
    }
}